package com.wismay.gc.elfinder.controller.executor;

import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.wismay.gc.elfinder.service.FsService;

public abstract class AbstractJsonCommandExecutor extends AbstractCommandExecutor {
	@Override
	final public void execute(FsService fsService, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws Exception {
		JSONObject json = new JSONObject();
		try {
			execute(fsService, request, servletContext, json);
			// response.setContentType("application/json; charset=UTF-8");
			response.setContentType("text/html; charset=UTF-8");

			PrintWriter writer = response.getWriter();
			json.write(writer);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
			json.put("error", e.getMessage());
		}
	}

	protected abstract void execute(FsService fsService, HttpServletRequest request, ServletContext servletContext, JSONObject json) throws Exception;

}